package cn.edu.lingnan.service.impl;

import cn.edu.lingnan.domain.Dto.FindStudentDto;
import cn.edu.lingnan.domain.Dto.UpdateStudentDto;
import cn.edu.lingnan.domain.Vo.StudentVo;
import cn.edu.lingnan.domain.pojo.PageBean;
import cn.edu.lingnan.domain.pojo.Project;
import cn.edu.lingnan.domain.pojo.Teacher;
import cn.edu.lingnan.domain.pojo.academy;
import cn.edu.lingnan.mapper.ProjectMapper;
import cn.edu.lingnan.mapper.StudentMapper;
import cn.edu.lingnan.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ProjectMapper projectMapper;
    @Override
    public PageBean list(FindStudentDto findStudentDto) {
        PageHelper.startPage(findStudentDto.getPageNum(), findStudentDto.getPageSize());
        Page<StudentVo> page =  studentMapper.list( findStudentDto );
        log.info("page:{}", page);

        long total = page.getTotal();
        log.info("total:{}", total);
        List< StudentVo > rows = page.getResult();

        return new PageBean(total, rows);
    }

    @Override
    public PageBean listTeacher(FindStudentDto findStudentDto) {
         PageHelper.startPage(findStudentDto.getPageNum(), findStudentDto.getPageSize());
         //处理前后空格
        if(findStudentDto.getUserName()!=null){
            findStudentDto.setUserName(findStudentDto.getUserName().trim());

        }
        if(findStudentDto.getProfessional()!=null){
            findStudentDto.setProfessional(findStudentDto.getProfessional().trim());

        }
         Page<Teacher> page =  studentMapper.listTeacher( findStudentDto );
         log.info("page:{}", page);

         long total = page.getTotal();
         log.info("total:{}", total);
         List< Teacher > rows = page.getResult();

         return new PageBean(total, rows);

    }

    @Override
    public Integer deleteStudent(Integer userId) {
        //查询是否是负责人或者项目成员
        Integer count1= projectMapper.listProjectByStudentId(userId);
        Integer count2=    projectMapper.listProjectMemberByStudentId(userId);
        if(count1>0 || count2>0){
            return  0;
        }
        studentMapper.deleteStudent(userId);
        return 1;
    }

    @Override
    public List<academy> findAcademy() {
        return studentMapper.findAcademy();
    }

    @Override
    public void updateStudent(UpdateStudentDto updateStudentDto) {

        studentMapper.updateStudent(updateStudentDto);
    }

    @Override
    public StudentVo findStudentById(Integer userId) {
        return  studentMapper.findStudentById(userId);
    }

    @Override
    public void freezeOrUnfreeze(Integer userId, Integer status) {
        studentMapper.freezeOrUnfreeze(userId, status);
    }

    @Override
    public PageBean listAccount(FindStudentDto findStudentDto) {
        PageHelper.startPage(findStudentDto.getPageNum(), findStudentDto.getPageSize());
        Page<StudentVo> page =  studentMapper.listAccount( findStudentDto );
        log.info("page:{}", page);

        long total = page.getTotal();
        log.info("total:{}", total);
        List< StudentVo > rows = page.getResult();

        return new PageBean(total, rows);
    }
}
